package com.lh.runmanage.service.Impl;

import com.lh.runmanage.dao.ProjectMapper;
import com.lh.runmanage.dto.ProjectDto;
import com.lh.runmanage.model.*;
import com.lh.runmanage.model.InPutParam.*;
import com.lh.runmanage.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-07-12 14:43
 * @function
 * @editLog
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    ProjectMapper projectMapper;

    /**
     * 查询Token---管理端表格
     *
     * @param projName 项目名称
     * @return 返回项目全信息
     */
    @Override
    public List<ProjectModel> selectProjectByPageGrid(String projName) {
        return projectMapper.selectProjectByPageGrid(projName);
    }

    /**
     * 分页得到前20项目坐标列表
     *
     * @return 返回前20项目坐标列表
     */
    @Override
    public List<ProjectSiteModel> selectProjectSite(List<String> id) {
        return projectMapper.selectProjectSite(id);
    }


    /**
     * 加个新项目
     *
     * @param projectInParam com.lh.runmanage.model.InPutParam.ProjectInParam
     * @return 影响条件
     */
    @Override
    public int insertProjectAll(ProjectInParam projectInParam) {
        if (projectMapper.insertProjectAllBeforeCheck(projectInParam) == 0)
            return projectMapper.insertProjectAll(projectInParam);
        else
            return -1000;
    }

    /**
     * 加个新项目
     *
     * @param projectInParam com.lh.runmanage.model.InPutParam.ProjectInParam
     * @return 影响条件
     */
    @Override
    public int insertProjectAllBeforeCheck(ProjectInParam projectInParam) {
        return projectMapper.insertProjectAllBeforeCheck(projectInParam);
    }

    /**
     * 修改项目基础信息
     *
     * @param projectInParam com.lh.runmanage.model.InPutParam.ProjectInParam
     * @return 影响条件
     */
    @Override
    public int updateProjectById(ProjectInParam projectInParam) {
        if (projectMapper.updateProjectByIdBeforeCheck(projectInParam) == 0)
            return projectMapper.updateProjectById(projectInParam);
        else
            return -1000;
    }

    /**
     * 修改项目基础信息
     *
     * @param projectInParam com.lh.runmanage.model.InPutParam.ProjectInParam
     * @return 影响条件
     */
    @Override
    public int updateProjectByIdBeforeCheck(ProjectInParam projectInParam) {
        return projectMapper.updateProjectByIdBeforeCheck(projectInParam);
    }

    /**
     * 删除项目及其它相关信息
     *
     * @param projectInParam com.lh.runmanage.model.InPutParam.ProjectInParam
     * @return 影响条件
     */
    @Override
    public int deleteProjectById(ProjectInParam projectInParam) {
        final int i = projectMapper.deleteProjectById(projectInParam);
        if (i > 0) {
            final String value = projectInParam.getId();
            String tableName, fieldName;
            tableName = "menu_authority";
            fieldName = "projectCode";
            deleteTable(tableName, fieldName, value);

            tableName = "menu_jump";
            deleteTable(tableName, fieldName, value);

            tableName = "report_systemUser";
            deleteTable(tableName, fieldName, value);

            tableName = "vou_col";
            deleteTable(tableName, fieldName, value);

            tableName = "vou_menu";
            deleteTable(tableName, fieldName, value);

            tableName = "vou_useTimeLength";
            deleteTable(tableName, fieldName, value);

            tableName = "vou_productAndProject";
            fieldName = "projCode";
            deleteTable(tableName, fieldName, value);

            tableName = "vou_projAvg";
            fieldName = "proCode";
            deleteTable(tableName, fieldName, value);
        }
        return i;
    }

    @Override
    public TotalProjectModel selectReportProjectTotal() {
        return projectMapper.selectReportProjectTotal();
    }

    private int deleteTable(String tableName, String fieldName, String value) {
        DeleteInParam deleteInParam = new DeleteInParam();
        deleteInParam.setTableName(tableName);
        deleteInParam.setFieldName(fieldName);
        deleteInParam.setValue(value);
        return projectMapper.deleteTableByField(deleteInParam);
    }

    /**
     * 修改项目Token
     *
     * @param projectInParam com.lh.runmanage.model.InPutParam.ProjectInParam
     * @return 影响条件
     */
    @Override
    public int updateProjectTokenById(ProjectInParam projectInParam) {
        if (projectMapper.updateProjectTokenByIdBeforeCheck(projectInParam) == 0)
            return projectMapper.updateProjectTokenById(projectInParam);
        else
            return -1000;
    }

    /**
     * 修改项目Token
     *
     * @param projectInParam com.lh.runmanage.model.InPutParam.ProjectInParam
     * @return 影响条件
     */
    @Override
    public int updateProjectTokenByIdBeforeCheck(ProjectInParam projectInParam) {
        return projectMapper.updateProjectTokenByIdBeforeCheck(projectInParam);
    }

    /**
     * 修改项目坐标
     *
     * @param projectInParam com.lh.runmanage.model.InPutParam.ProjectInParam
     * @return 影响条件
     */
    @Override
    public int updateProjectSiteById(ProjectInParam projectInParam) {
        return projectMapper.updateProjectSiteById(projectInParam);
    }

    /**
     * 修改项目统计类型
     *
     * @param projectInParam com.lh.runmanage.model.InPutParam.ProjectInParam
     * @return 影响条件
     */
    @Override
    public int updateProjectTotalTypeById(ProjectInParam projectInParam) {
        return projectMapper.updateProjectTotalTypeById(projectInParam);
    }

    /**
     * 得到项目、产品、菜单关系树状数据
     *
     * @param menuJumpInParam 主键
     * @return 树
     */
    @Override
    public List<ProjectDto> selectProjectTreeByProjectId(MenuJumpInParam menuJumpInParam) {
        return projectMapper.selectProjectTreeByProjectId(menuJumpInParam);
    }

    @Override
    public List<ProjectDto> selectProjectTreeByProjectIdList(MenuFilterModel menuFilterModel) {
        return projectMapper.selectProjectTreeByProjectIdList(menuFilterModel);
    }

    @Override
    public TotalProjectModel selectProjectTotalByCreateDate(Date createDate) {
        return projectMapper.selectProjectTotalByCreateDate(createDate);
    }

    @Override
    public TotalProjectModel2 selectD2() {
        return projectMapper.selectD2();
    }

    /**
     * 根据项目ID，得到项目基础信息
     *
     * @param id 主键
     * @return 项目基础信息
     */
    @Override
    public ProjectBaseAllModel selectProjectBaseById(String id) {
        return projectMapper.selectProjectBaseById(id);
    }

    @Override
    public ProjectTokenModel selectProjectTokenById(String id) {
        return projectMapper.selectProjectTokenById(id);
    }

    /**
     * 展示端方法：D4
     *
     * @return 得到前20个项目列表
     */
    @Override
    public List<ProjectD4Model> selectProjectD4OrderBy() {
        return projectMapper.selectProjectD4OrderBy();
    }

    /**
     * 展示端方法：T2，甘特图
     *
     * @param projectGantuInParam com.lh.runmanage.model.InPutParam.ProjectGantuInParam
     * @return 项目列表
     */
    @Override
    public List<ProjectGantuModel> selectProjectGantuChart(ProjectGantuInParam projectGantuInParam) {
        return projectMapper.selectProjectGantuChart(projectGantuInParam);
    }

    /**
     * 展示端方法：D5
     *
     * @param proCode 项目名称
     * @return 得到前20个项目列表
     */
    @Override
    public List<ProjectD4Model> selectProjectD5(List<String> proCode) {
        return projectMapper.selectProjectD5(proCode);
    }

    /**
     * 展示端方法：T2
     *
     * @param projAvgD8InParam 实施开始日期
     * @return 根据实施时间查询
     */
    @Override
    public List<ProjectD4Model> selectProjectT2OrderTime(ProjAvgD8InParam projAvgD8InParam) {
        return projectMapper.selectProjectT2OrderTime(projAvgD8InParam);
    }

    /**
     * 展示端方法：P5
     *
     * @param id 主键
     * @return 得到前20个项目列表
     */
    @Override
    public List<ProjectD4Model> selectProjectP5(List<String> id) {
        return projectMapper.selectProjectP5(id);
    }

    @Override
    public ProjectP5Model selectProjectNameByProjectId(String projectCode) {
        return projectMapper.selectProjectNameByProjectId(projectCode);
    }

}